//
//  LHRecommendRestaurantTableView.m
//  Knight_Tour
//
//  Created by 庞魁 on 15-5-15.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHRecommendRestaurantTableView.h"
#import "LHRecommendResTableViewCell.h"
#import "LHNetworkHandle.h"
#import "LHRecommendRestaurantModel.h"
#import "UIScrollView+MJRefresh.h"

@interface LHRecommendRestaurantTableView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *resTableView;
@property (nonatomic, retain) NSMutableArray *resArr;
@property (nonatomic, retain) NSMutableArray *tempArr;


@end

@implementation LHRecommendRestaurantTableView

- (void)dealloc
{
    [_resTableView release];
    [_resArr release];
    [_tempArr release];
    [super dealloc];
}


- (instancetype)initWithFrame:(CGRect)frame cityId:(NSString *)cityId
{
    if (self = [super initWithFrame:frame]) {
        
        self.tempArr = [NSMutableArray array];
        
        [self getDataWithCityId:cityId];
        
    }
    
    
    return self;
}

/**
 *  创建推荐餐馆的tableView
 */

- (void)createRecommendResTableView
{
    self.resTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
    
    _resTableView.delegate = self;
    _resTableView.dataSource = self;
    
    [_resTableView addFooterWithTarget:self action:@selector(freshData)];
    [_resTableView setFooterRefreshingText:@"客官,别心急嘛"];
    
    [_resTableView addHeaderWithTarget:self action:@selector(tempData)];
    [_resTableView setHeaderRefreshingText:@"我正在用力~~~"];
    
    [self addSubview:_resTableView];
    
    [_resTableView release];
    
}

#pragma mark - tableView协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tempArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LHRecommendResTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tt"];
    
    if (cell == nil) {
        
        cell = [[[LHRecommendResTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tt"]autorelease];
    }
    
    cell.recommendRes = _tempArr[indexPath.row];
    
    cell.backgroundColor = [UIColor colorWithRed:0.871 green:0.962 blue:0.965 alpha:1.000];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self.delegate jumpToResDetailWithIndexPath:indexPath resArr:_tempArr];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.frame.size.width / 3 + 50;
}

// 获取网络数据
- (void)getDataWithCityId:(NSString *)cityId
{
    NSString *r = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/3/%@/pois/restaurant/?start=0&count=40&sort=default&shift=false&latitude=38.8826495629487&longitude=121.53969115009306", cityId];
    
    [LHNetworkHandle getDataWithURL:r completion:^(id result) {
        
        self.resArr = [NSMutableArray array];
        
        NSArray *arr = [result objectForKey:@"items"];
        
        for (NSDictionary *tempDic in arr) {
            
            LHRecommendRestaurantModel *resModel = [[LHRecommendRestaurantModel alloc] initWithDic:tempDic];
            
            [_resArr addObject:resModel];
            
            [resModel release];
            
        }
        
        for (NSInteger i = 0; i < 10; i++) {
            
            [self.tempArr addObject:self.resArr[i]];
        }
        
        [self createRecommendResTableView];
        
        
    }];
    
    
    
    
}

- (void)freshData
{
    if (self.tempArr.count != self.resArr.count) {
        
        NSInteger j = self.tempArr.count;
        
        for (NSInteger i = j; i < j + 5; i ++) {
            
            [self.tempArr addObject:self.resArr[i]];
        }
    }
    
    [_resTableView reloadData];
    [_resTableView footerEndRefreshing];
}

- (void)tempData
{
    [_resTableView reloadData];
    [_resTableView headerEndRefreshing];
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
